package com.anyi.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.anyi.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.anyi.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.anyi.gulimall.product.entity.ProductAttrValueEntity;
import com.anyi.gulimall.product.service.AttrAttrgroupRelationService;
import com.anyi.gulimall.product.vo.AttrRespVo;
import com.anyi.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.anyi.gulimall.product.entity.AttrEntity;
import com.anyi.gulimall.product.service.AttrService;
import com.anyi.gulimall.common.utils.PageUtils;
import com.anyi.gulimall.common.utils.R;



/**
 * 商品属性
 *
 * @author anyi
 * @email 1961741003@qq.com
 * @date 2022-07-27 10:58:39
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;



    /**
     * 列表
     */
    @RequestMapping("/{attrType}/list/{categoryId}")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long categoryId ,@PathVariable String attrType){
        PageUtils page = attrService.queryArrtPage(params,categoryId,attrType);
        return R.ok().put("page", page);
    }


    @GetMapping("/base/listforspu/{spuId}")
    // @RequiresPermissions("product:attr:list")
    public R getListForSpu(@PathVariable Long spuId){
        List<ProductAttrValueEntity> list = attrService.getListForSpu(spuId);
        return R.ok().put("data", list);
    }

    @PostMapping("/update/{spuId}")
    // @RequiresPermissions("product:attr:list")
    public R updateSpuAttr(@PathVariable Long spuId,@RequestBody List<ProductAttrValueEntity> list){
        attrService.updateSpuAttr(spuId,list);
        return R.ok();
    }




    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){

		attrService.updateAttrEntity(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
